//
//  LHUpdateAppView.m
//  Loohoo
//
//  Created by 陈镇 on 2025/2/20.
//

#import "LHUpdateAppView.h"


@interface LHUpdateAppView ()

@property(nonatomic,strong) NSDictionary *data;
@property (nonatomic, strong) void (^confirmBlock)(int status);
@end
@implementation LHUpdateAppView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.disableBackgroundDismissAction = YES;
        self.backgroundView.backgroundColor = UIColorHex(#00000044);
    }
    return self;
}

+ (void)openWithInfomation:(NSDictionary *)infomation confirmBlock:(void (^)(int))comfirmBlock {
    LHUpdateAppView *view = [[LHUpdateAppView alloc] init];
    view.data = infomation;
    view.confirmBlock = comfirmBlock;
    [view createUI];
    [view showInView:SLWindow animation:NO dismissBlock:nil];
}

-(void)createUI{
    
    
    NSString *version_text = self.data[@"version"];
    NSString *desc_text = self.data[@"change_log"];
    
    
    CGFloat width = SCREEN_WIDTH - 80;
    
    
    UIImageView *topView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, width, width * 142.0 / 290)];
    topView.image = [UIImage imageNamed:@"bg_update_bg"];
    [self.contentView addSubview:topView];
    
    QMUILabel *title = [QMUILabel createTextColor:UIColor.whiteColor font:SLFontMakeMedium(25) textAlignment:NSTextAlignmentLeft];
    title.text = @"发现新版本";
    [self.contentView addSubview:title];
    title.frame = CGRectMake(MYDistance(26*2), MYDistance(30*2), 200, 26);
    
    QMUILabel *version = [QMUILabel createTextColor:CZMainColor font:SLFontMakeRegular(12) textAlignment:NSTextAlignmentCenter];
    version.text = [NSString stringWithFormat:@"v%@",version_text];
    [self.contentView addSubview:version];
    version.backgroundColor = UIColor.whiteColor;
    [version sizeToFit];
    version.frame = CGRectMake(MYDistance(26*2), MYDistance(76*2), version.width + 20, MYDistance(20*2));
    version.layer.cornerRadius = version.height/2;
    version.layer.masksToBounds = YES;
    
    
    QMUILabel *desc = [QMUILabel createTextColor:UIColorHex(333333) font:SLFontMakeRegular(14) textAlignment:NSTextAlignmentLeft];
    desc.numberOfLines = 0;
    desc.text = desc_text;
    [self.contentView addSubview:desc];
    
    CGFloat height = [desc.text sizeWithFont:desc.font constrainedToSize:CGSizeMake(width - 2*MYDistance(20*2) , CGFLOAT_MAX)].height;
    desc.frame = CGRectMake(MYDistance(20*2), topView.bottom + MYDistance(20*2), width - 2*MYDistance(20*2), height);
    
    
    
    
    QMUIButton *btn = [QMUIButton createButtonTitle:@"立即更新" titleColor:UIColor.whiteColor font:SLFontMakeRegular(14)];
    btn.backgroundColor = CZMainColor;
    [self.contentView addSubview:btn];
    
    btn.frame = CGRectMake((width - MYDistance(165*2))/2, desc.bottom + MYDistance(30*2), MYDistance(165*2), MYDistance(40*2));
    btn.layer.cornerRadius = btn.height/2;
    btn.layer.masksToBounds = YES;
    [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
 
    
    
    int t = [self.data[@"update_type"] intValue];
    
    if (t == 1) { //可选
        QMUIButton *cancel = [QMUIButton createButtonTitle:@"下次更新" titleColor:UIColorHex(#666666) font:SLFontMakeRegular(14)];
        [self.contentView addSubview:cancel];
        [cancel addTarget:self action:@selector(cancelClick) forControlEvents:UIControlEventTouchUpInside];
        cancel.frame = CGRectMake((width - 70)/2, btn.bottom + 5, 70, 40);
        
        self.contentView.frame = CGRectMake(40, SCREEN_HEIGHT/2 - (cancel.bottom + 10)/2 - 20,width, cancel.bottom + 10);
        
 
    }else{//强制更新
        self.contentView.frame = CGRectMake(40, SCREEN_HEIGHT/2 - (btn.bottom + 10)/2 - 20,width, btn.bottom + 10);
        
    }
    
    self.contentView.layer.cornerRadius = 16;
    self.contentView.layer.masksToBounds = YES;

}



-(void)cancelClick{
    
    [self dismiss];
    self.confirmBlock(0);
}

-(void)btnClick{
    NSURL *url = [NSURL URLWithString:@"https://apps.apple.com/app/id6739098855"];
    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
    }];
    
}
@end
